package com.magicodex.bookkeeping.biz.setting.event;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.view.View;
import android.widget.Toast;

import com.magicodex.bookkeeping.R;
import com.magicodex.bookkeeping.biz.accounting.constant.AccountingConstants;
import com.magicodex.bookkeeping.biz.accounting.dto.AccountingDTO;
import com.magicodex.bookkeeping.biz.accounting.util.AccountingCsvUtil;
import com.magicodex.bookkeeping.biz.accounting.util.AccountingJsonUtil;
import com.magicodex.bookkeeping.biz.common.util.CheckUtil;
import com.magicodex.bookkeeping.biz.common.util.FileUtil;

import java.util.Collections;
import java.util.List;

/**
 * @author mh.z
 */
public class ExportButtonClickListener implements View.OnClickListener {
    private Activity activity;

    public ExportButtonClickListener(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onClick(View view) {
        String json = FileUtil.readFile(activity, AccountingConstants.DATA_PATH);
        List<AccountingDTO> recordList = (json != null)
                ? AccountingJsonUtil.parseAccountingDTOs(json)
                : Collections.emptyList();
        String content = AccountingCsvUtil.stringifyAccountingDTOs(recordList);

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(R.string.dialog_title_export_data);
        builder.setMessage(content);
        builder.setPositiveButton(R.string.button_copy, createPositiveButtonClickListener(content))
                .setNegativeButton(R.string.button_cancel, createNegativeButtonClickListener());

        AlertDialog dialog = builder.create();
        // 显示对话框
        dialog.show();
    }

    /**
     * 点击确定按钮的事件监听器
     *
     * @return
     */
    protected DialogInterface.OnClickListener createPositiveButtonClickListener(String content) {
        CheckUtil.notNull(content, "content null");

        return (dialog, which) -> {
            Resources resources = activity.getResources();
            String label = resources.getString(R.string.clip_label_export_data);

            // 把内容放进剪贴板
            ClipData clipData = ClipData.newPlainText(label, content);
            ClipboardManager clipboardManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
            clipboardManager.setPrimaryClip(clipData);

            // 提示复制成功
            String copySuccessMsg = resources.getString(R.string.msg_copy_success);
            Toast toast = Toast.makeText(activity, copySuccessMsg, Toast.LENGTH_SHORT);
            toast.show();
        };
    }

    /**
     * 点击取消按钮的事件监听器
     *
     * @return
     */
    protected DialogInterface.OnClickListener createNegativeButtonClickListener() {
        return (dialog, which) -> {
            //
        };
    }

}
